<template>
    <div class="inner-warp">
        <MainTitleVue title="我的成果" />
        <n-data-table :columns="columns" :data="data" :pagination="pagination" />
    </div>
</template>

<script setup lang='ts'>
import MainTitleVue from "@/components/common/MainTitle.vue";
import { NButton, NTag, useMessage } from "naive-ui";
import { h } from "@vue/runtime-core";
import { getCurrentInstance, onMounted, ref } from "vue";
import { useRouter } from 'vue-router';

// api访问代理
const { proxy } = getCurrentInstance() as any

// 引入消息函数
const message = useMessage()

// 引入路由
const router = useRouter()

const pagination = { pageSize: 10 }

const columns = [
    {
        title: '论文',
        key: 'title',
        align: 'center'
    },
    {
        title: '期刊/会议',
        key: 'venue',
        align: 'center',
    },
    {
        title: '作者',
        key: 'author',
        align: 'center',
        render(row: any) {
            const tags = row.author.map((tagKey: any) => {
                return h(
                    NTag,
                    {
                        style: {
                            marginRight: '6px'
                        },
                        type: 'primary'
                    },
                    {
                        default: () => tagKey
                    }
                )
            })
            return tags
        }
    },
    {
        title: '操作',
        key: 'opitons',
        align: 'center',
        render(row: any) {
            return h(
                NButton,
                {
                    size: 'small',
                    type: 'warning',
                    style: {
                        marginRight: '6px'
                    },
                    onClick: () => toPaper(row)
                },
                { default: () => '查看' }
            )
        }
    }
]

let data = ref([])

onMounted(() => {
    init()
})

const init = (() => {
    proxy.$api.user.getMyPaperList().then((result: any) => {
        if (result.success) {
            data.value = result.data
        } else {
            message.warning(result.msg)
        }
    }).catch(() => {
        message.error('未知错误')
    });
})

// 查看论文
const toPaper = ((row: any) => {
    let router_data = router.resolve({ name: 'PaperIntro', params: { id: row.id } })
    window.open(router_data.href, '_blank')
})

</script>

<style scoped>
.inner-warp {
    margin: 50px auto;
    width: 1100px;
}
</style>